package androidx.test.espresso.util;

import android.view.View;
import android.view.ViewGroup;
import androidx.test.espresso.core.internal.deps.guava.base.Function;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import androidx.test.espresso.core.internal.deps.guava.collect.AbstractIterator;
import androidx.test.espresso.core.internal.deps.guava.collect.Iterables;
import androidx.test.espresso.core.internal.deps.guava.collect.Lists;
import androidx.test.espresso.core.internal.deps.guava.collect.Maps;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

/* loaded from: classes.dex */
public final class TreeIterables {
    private static final TreeViewer<View> VIEW_TREE_VIEWER = new ViewTreeViewer();

    /* loaded from: classes.dex */
    public interface TreeViewer<T> {
        Collection<T> children(T instance);
    }

    private TreeIterables() {
    }

    public static Iterable<ViewAndDistance> depthFirstViewTraversalWithDistance(View root) {
        final DistanceRecordingTreeViewer distanceRecordingTreeViewer = new DistanceRecordingTreeViewer(root, VIEW_TREE_VIEWER);
        return Iterables.transform(depthFirstTraversal(root, distanceRecordingTreeViewer), new Function<View, ViewAndDistance>() { // from class: androidx.test.espresso.util.TreeIterables.1
            @Override // androidx.test.espresso.core.internal.deps.guava.base.Function
            public ViewAndDistance apply(View view) {
                return new ViewAndDistance(view, distanceRecordingTreeViewer.getDistance(view));
            }
        });
    }

    public static Iterable<View> depthFirstViewTraversal(View root) {
        return depthFirstTraversal(root, VIEW_TREE_VIEWER);
    }

    public static Iterable<View> breadthFirstViewTraversal(View root) {
        return breadthFirstTraversal(root, VIEW_TREE_VIEWER);
    }

    static <T> Iterable<T> depthFirstTraversal(final T root, final TreeViewer<T> viewer) {
        Preconditions.checkNotNull(root);
        Preconditions.checkNotNull(viewer);
        return new TreeTraversalIterable(root, TraversalStrategy.DEPTH_FIRST, viewer);
    }

    static <T> Iterable<T> breadthFirstTraversal(final T root, final TreeViewer<T> viewer) {
        Preconditions.checkNotNull(root);
        Preconditions.checkNotNull(viewer);
        return new TreeTraversalIterable(root, TraversalStrategy.BREADTH_FIRST, viewer);
    }

    /* loaded from: classes.dex */
    public static class TreeTraversalIterable<T> implements Iterable<T> {
        private final T root;
        private final TraversalStrategy traversalStrategy;
        private final TreeViewer<T> treeViewer;

        private TreeTraversalIterable(T root, TraversalStrategy traversalStrategy, TreeViewer<T> treeViewer) {
            this.root = (T) Preconditions.checkNotNull(root);
            this.traversalStrategy = (TraversalStrategy) Preconditions.checkNotNull(traversalStrategy);
            this.treeViewer = (TreeViewer) Preconditions.checkNotNull(treeViewer);
        }

        @Override // java.lang.Iterable
        public Iterator<T> iterator() {
            final LinkedList newLinkedList = Lists.newLinkedList();
            newLinkedList.add(this.root);
            return new AbstractIterator<T>() { // from class: androidx.test.espresso.util.TreeIterables.TreeTraversalIterable.1
                {
                    TreeTraversalIterable.this = this;
                }

                @Override // androidx.test.espresso.core.internal.deps.guava.collect.AbstractIterator
                public T computeNext() {
                    if (!newLinkedList.isEmpty()) {
                        T t = (T) Preconditions.checkNotNull(TreeTraversalIterable.this.traversalStrategy.next(newLinkedList), "Null items not allowed!");
                        TreeTraversalIterable.this.traversalStrategy.combineNewChildren(newLinkedList, TreeTraversalIterable.this.treeViewer.children(t));
                        return t;
                    }
                    return endOfData();
                }
            };
        }
    }

    /* loaded from: classes.dex */
    public enum TraversalStrategy {
        BREADTH_FIRST { // from class: androidx.test.espresso.util.TreeIterables.TraversalStrategy.1
            /* JADX WARN: Multi-variable type inference failed */
            @Override // androidx.test.espresso.util.TreeIterables.TraversalStrategy
            <T> void combineNewChildren(LinkedList<T> nodes, Collection<T> newChildren) {
                nodes.addAll(newChildren);
            }
        },
        DEPTH_FIRST { // from class: androidx.test.espresso.util.TreeIterables.TraversalStrategy.2
            /* JADX WARN: Multi-variable type inference failed */
            @Override // androidx.test.espresso.util.TreeIterables.TraversalStrategy
            <T> void combineNewChildren(LinkedList<T> nodes, Collection<T> newChildren) {
                nodes.addAll(0, newChildren);
            }
        };

        abstract <T> void combineNewChildren(LinkedList<T> nodes, Collection<T> newChildren);

        <T> T next(LinkedList<T> nodes) {
            return nodes.removeFirst();
        }
    }

    /* loaded from: classes.dex */
    static class ViewTreeViewer implements TreeViewer<View> {
        ViewTreeViewer() {
        }

        @Override // androidx.test.espresso.util.TreeIterables.TreeViewer
        public Collection<View> children(View view) {
            Preconditions.checkNotNull(view);
            if (view instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) view;
                int childCount = viewGroup.getChildCount();
                ArrayList newArrayList = Lists.newArrayList();
                for (int i = 0; i < childCount; i++) {
                    newArrayList.add(viewGroup.getChildAt(i));
                }
                return newArrayList;
            }
            return Collections.emptyList();
        }
    }

    /* loaded from: classes.dex */
    public static class DistanceRecordingTreeViewer<T> implements TreeViewer<T> {
        private final TreeViewer<T> delegateViewer;
        private final Map<T, Integer> nodeToDistance = Maps.newHashMap();
        private final T root;

        DistanceRecordingTreeViewer(T root, TreeViewer<T> delegateViewer) {
            this.root = (T) Preconditions.checkNotNull(root);
            this.delegateViewer = (TreeViewer) Preconditions.checkNotNull(delegateViewer);
        }

        int getDistance(T node) {
            return ((Integer) Preconditions.checkNotNull(this.nodeToDistance.get(node), "Never seen %s before", node)).intValue();
        }

        @Override // androidx.test.espresso.util.TreeIterables.TreeViewer
        public Collection<T> children(final T node) {
            if (node == this.root) {
                this.nodeToDistance.put(node, 0);
            }
            int distance = getDistance(node) + 1;
            Collection<T> children = this.delegateViewer.children(node);
            for (T t : children) {
                this.nodeToDistance.put(t, Integer.valueOf(distance));
            }
            return children;
        }
    }

    /* loaded from: classes.dex */
    public static class ViewAndDistance {
        private final int distanceFromRoot;
        private final View view;

        private ViewAndDistance(View view, int distanceFromRoot) {
            this.view = view;
            this.distanceFromRoot = distanceFromRoot;
        }

        public View getView() {
            return this.view;
        }

        public int getDistanceFromRoot() {
            return this.distanceFromRoot;
        }
    }
}
